package cs2340.g21.wms;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ItemPageActivity extends Activity {
	
	private static final String LOG_TAG = "debugger";
    private Item item;
    private ArrayList<Item> items;
    private ArrayList<Item> original;
    private Coordinator coordinator;
    
    // Item attributes
    private String mName;
    private SerializableBitmap mPicture;
    private String mDescription;
    private List<String> mTags;
    private Calendar mDate;
    private SerializableLocation mLocation;
    
    // UI elements
    private ImageView mPhotoView;
    private TextView mNameView;
    private TextView mDescriptionView;
    private TextView mTagsView;
    private TextView mDateView;
    private TextView mLocationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_page);
        
        coordinator = new Coordinator();
        if(getIntent().getExtras() != null){
            coordinator = (Coordinator) getIntent().getExtras().getSerializable("cs2340.g21.wms.coordinator");
        }
        
        item = (Item) getIntent().getExtras().get("item");
        items = (ArrayList<Item>)getIntent().getExtras().get("items");
        original = (ArrayList<Item>)getIntent().getExtras().get("original");
        // Get details about the item
        mName = item.getName();
        mPicture = item.getPicture();
        mDescription = item.getDescription();
        mTags = item.getTags();
        mDate = item.getDate();
        mLocation = item.getLocation();
        
        // Get the references to the UI components
        mPhotoView = (ImageView) findViewById(R.id.item_page_image);
        mNameView = (TextView) findViewById(R.id.item_page_name);
        mDescriptionView = (TextView) findViewById(R.id.item_page_description);
        mTagsView = (TextView) findViewById(R.id.item_page_tags);
        mDateView = (TextView) findViewById(R.id.item_page_date);
        mLocationView = (TextView) findViewById(R.id.item_page_location);
        
        setUIComponents();
    }
    
    private void setUIComponents() {
    	// Set the appropriate fields in the UI components
    	if (mPicture != null) {
    		mPhotoView.setImageBitmap(mPicture.getBitmap());
    	}
        if (mName != null) {
        	mNameView.setText(mName);
        }
        if (mDescription != null) {
        	mDescriptionView.setText(getString(R.string.description) + ": " + mDescription);
        }
        if (mTags != null) {
	        String tags = "";
	        for (String t : mTags) {
	        	tags += (t + ", ");
	        }
	        if (tags.length() > 2) {
	        	tags = tags.substring(0, tags.length()-2);
	        }
        	mTagsView.setText(getString(R.string.item_tags) + ": " + tags);
        }
        if (mDate != null) {
        	mDateView.setText(mDate.get(Calendar.DAY_OF_MONTH) + "/" + (mDate.get(Calendar.MONTH)+1));
        }
        if (mLocation != null) {
        	mLocationView.setText(mLocation.getLocality());
        }
    }
    
    public void onBackPressed(View view) {
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.putExtra("items", items);
        intent.putExtra("original", original);
        intent.setClass(ItemPageActivity.this, ItemListActivity.class);
        startActivity(intent);
        finish();
    }
    
    public void messageUser(View view){
        AlertDialog.Builder notifyBuild = new AlertDialog.Builder(this);
        notifyBuild.setTitle("Contact user");
        final EditText input = new EditText(this);
        //String email = getIntent().getStringExtra(EXTRA_EMAIL);
        //input.setText(email);
        notifyBuild.setView(input);
        
        notifyBuild.setPositiveButton(R.string.prompt_confirm, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String message = input.getText().toString();
                Note n = new Note(message, coordinator.getAccount((item.getUserId())),coordinator.getUser(), Calendar.getInstance());
                coordinator.getAccount(item.getUserId()).addNote(n);
            }
        });
        
        notifyBuild.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
        notifyBuild.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_item_page, menu);
        return true;
    }

}
